package com.oygo.hunter.utils;

import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class EncTuils {


    private static final String AES = "AESCrypt";
    public static boolean isEncode = false;
    private static final String AESTYPE = "AES/CBC/PKCS7Padding";
    private static final String SHA256 = "SHA-256";
    private static byte[] keyInit = null;
    private static final String TYPE = "UTF-8";

    static {
        keyInit = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        isEncode = false;
    }

    public static String decode(String arg1, String arg2) throws GeneralSecurityException {
        try {
            return new String(EncTuils.cipher(EncTuils.getAesKey(arg1), EncTuils.keyInit, Base64.decode(arg2, 2)), "UTF-8");
        }
        catch(UnsupportedEncodingException v1) {
            throw new GeneralSecurityException(v1);
        }
    }

    private static String byte2String(byte[] arg6) {
        char[] v0 = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        char[] v1 = new char[arg6.length * 2];
        int v2;
        for(v2 = 0; v2 < arg6.length; ++v2) {
            int v3 = arg6[v2] & 0xFF;
            int v4 = v2 * 2;
            v1[v4] = v0[v3 >>> 4];
            v1[v4 + 1] = v0[v3 & 15];
        }

        return new String(v1);
    }

    private static SecretKeySpec getAesKey(String arg3) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest v0 = MessageDigest.getInstance("SHA-256");
        byte[] v3 = arg3.getBytes("UTF-8");
        v0.update(v3, 0, v3.length);
        return new SecretKeySpec(v0.digest(), "AES");
    }

    public static byte[] cipher(SecretKeySpec arg2, byte[] arg3, byte[] arg4) throws GeneralSecurityException {
        Cipher v0 = Cipher.getInstance("AES/CBC/PKCS7Padding");
        v0.init(2, arg2, new IvParameterSpec(arg3));
        return v0.doFinal(arg4);
    }

    public static String encode(String arg2, String arg3) throws GeneralSecurityException {
        try {
            return Base64.encodeToString(EncTuils.cbc(EncTuils.getAesKey(arg2), EncTuils.keyInit, arg3.getBytes("UTF-8")), 2);
        }
        catch(UnsupportedEncodingException v2) {
            throw new GeneralSecurityException(v2);
        }
    }

    public static byte[] cbc(SecretKeySpec arg2, byte[] arg3, byte[] arg4) throws GeneralSecurityException {
        Cipher v0 = Cipher.getInstance("AES/CBC/PKCS7Padding");
        v0.init(1, arg2, new IvParameterSpec(arg3));
        return v0.doFinal(arg4);
    }

}
